import tkinter as tk
from tkinter import ttk


class EditableListView:
    def __init__(self, root):
        self.root = root
        self.root.title("可编辑Listview示例")

        # 创建Treeview作为列表视图
        self.tree = ttk.Treeview(root, columns=('Name', 'Age', 'City'), show='headings')
        self.tree.heading('Name', text='姓名')
        self.tree.heading('Age', text='年龄')
        self.tree.heading('City', text='城市')

        # 添加示例数据
        self.tree.insert('', 'end', values=('张三', 25, '北京'))
        self.tree.insert('', 'end', values=('李四', 30, '上海'))
        self.tree.insert('', 'end', values=('王五', 35, '广州'))

        self.tree.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)

        # 绑定双击事件
        self.tree.bind('<Double-1>', self.on_double_click)
        # self.tree.bind('<Button-1>', self.on_double_click)
        # 用于跟踪当前编辑的条目和列
        self.entry = None
        self.current_item = None
        self.current_column = None

    def on_double_click(self, event):
        # 获取点击的区域
        region = self.tree.identify("region", event.x, event.y)

        # 只有在单元格区域才允许编辑
        if region == "cell":
            # 获取点击的条目和列
            item = self.tree.identify_row(event.y)
            column = self.tree.identify_column(event.x)
            print('item条目:',item, '列:', column)
            # 取消之前可能存在的编辑框
            self.cancel_edit()

            # 获取单元格的坐标和值
            x, y, width, height = self.tree.bbox(item, column)
            value = self.tree.set(item, column)

            # 创建输入框
            self.entry = tk.Entry(self.tree, justify='center')
            self.entry.insert(0, value)
            self.entry.select_range(0, tk.END)
            self.entry.place(x=x, y=y, width=width, height=height)

            # 绑定事件
            self.entry.bind('<Return>', self.on_enter)
            self.entry.bind('<Escape>', self.cancel_edit)
            self.entry.bind('<FocusOut>', self.on_focus_out)

            # 设置焦点
            self.entry.focus_set()

            # 保存当前编辑的条目和列
            self.current_item = item
            self.current_column = column

    def on_enter(self, event):
        self.save_edit()

    def on_focus_out(self, event):
        self.save_edit()

    def save_edit(self):
        if self.entry and self.current_item and self.current_column:
            # 获取新值
            new_value = self.entry.get()

            # 更新Treeview中的值
            column_index = int(self.current_column.replace('#', '')) - 1
            current_values = list(self.tree.item(self.current_item, 'values'))
            current_values[column_index] = new_value
            self.tree.item(self.current_item, values=current_values)

            # 销毁输入框
            self.entry.destroy()
            self.entry = None
            self.current_item = None
            self.current_column = None

    def cancel_edit(self, event=None):
        if self.entry:
            self.entry.destroy()
            self.entry = None
            self.current_item = None
            self.current_column = None


if __name__ == "__main__":
    root = tk.Tk()
    app = EditableListView(root)
    root.mainloop()